<?php

namespace app\home\controller;

use think\Session;

class User extends common
{
    public $UserCount=0;
    public $UserMoneySum = 0;
    public $UserOnLineSum = 0;
    
    /**
     * 用户信息
     * @return mixed|string
     */
    public function user(){
        //用户信息
        $user = session('user');
        if(!$user){
            $this->error('登录已过期，请重新登录!','login/login');
        }
        $this->assign('userinfo',$user);
        return $this->fetch("common/user_index");
    }
    
    /**
     * 团队管理
     * @return mixed|string
     */
    public function myteam(){
        return $this->fetch("common/myteam");
    }
    
    /**
     * 佣金转余额
     * @return number[]|string[]|mixed|string
     */
    public function myyj(){
        $user = session('user');
        if (request() -> isAjax()){
            $return = array();
            $money = input('money',"","float");
            if (empty($money) || $money <= 0){
                $return['err'] = 0;
                $return['msg'] = "金额输入有误！";
            }else {
                $dbuser = getUser($user['id']);
                if ($dbuser['rebate'] < $money){
                    $return['err'] = 0;
                    $return['msg'] = "没有足够的佣金！";
                }else {
                    db('user') -> where("id={$user['id']}") -> setDec("rebate",$money);
                    db('user') -> where("id={$user['id']}") -> setInc("money",$money);
                    $newUser = getUser($user['id']);
                    Session::set("user",$newUser);
                    $return['err'] = 1;
                    $return['msg'] = "转入成功！";
                    $return['money'] = $newUser['rebate'];
                }
            }
            return $return;
        }
        $this->assign("user",$user);
        return $this->fetch("common/myyj");
    }
    
    /**
     * 充值
     * @return mixed|string
     */
    public function mycz(){
        $user = session("user");
        $this->assign('user',$user);
        return $this->fetch("common/mycz");
    }
    
    /**
     * 提现
     * @return mixed|string
     */
    public function mytx(){
        $user = session("user");
        $this->assign('user',$user);
        return $this->fetch("common/mytx");
    }
    
    /**
     * 下注明细
     * @return mixed|string
     */
    public function myxz(){
        $id = session("user")['id'];
        $betData = db('bet')->where("uid={$id}")->order('expect')->select();
        $data = array();
        if ($betData){
            foreach ($betData as $k => $v){
                $data[$v['expect']][] = $v;
            }
            foreach ($data as $k => $v){
                $data[$k]['num'] = 0;
                $data[$k]['key'] = $k;
                $data[$k]['state'] = 1;
                $data[$k]['money'] = 0;
                foreach ($v as $item){
                    $data[$k]['state'] *= $item['state']; 
                    $data[$k]['money'] += $item['winning'];
                    $data[$k]['num'] += strlen($item['content'])*$item['number'];
                }
            }
        }
        $this->assign('betdata',$data);
        return $this->fetch("common/myxz");
    }
    
    /**
     * 添加会员
     * @return mixed|string
     */
    public function adduser(){
        $user = session('user');
        if (request() -> isPost()){
            $data = input();
            if (trim($data['pass']) !== trim($data['repass'])){
                $this->error("两次输入的密码不一致");
            }elseif ($user['rebate'] < trim($data['rebate'],"%")){
                $this->error("下级会员返利不得超出您当前的返利比（{$user['rebate']}%）！");
            } else {
                $insert = array(
                    'qq'    =>  trim($data['qq']),
                    'name'  =>  trim($data['name']),
                    'pass'  =>  md5(trim($data['pass'])),
                    'rebate'=>  trim($data['rebate'],"%"),
                    'lv'    =>  2,
                    'gx'    =>  $user['id'],
                    'come_type'=>2,
                    'login_time'=>time(),
                    'state' =>  1
                );
                $info = db('user')->insert($insert);
                if ($info){
                    $this->success("操作成功！");
                }else {
                    $this->error("操作失败！");
                }
            }
        }
        $this->assign('rebate',$user['rebate']);
        return $this->fetch("common/adduser");
    }
    
    /**
     * 会员列表
     * @return mixed|string
     */
    public function userlist(){
        $id = input('id',"",'intval');
        if (!$id){
            $user = session('user');
            $id = $user['id'];
        }
        $where = "";
        $name = input('name',"",'htmlspecialchars,trim');
        if ($name) $where['name'] = array('like', "%$name%");
        $userlist = db('user')->where("gx={$id}")->where($where)->select();
        $this->assign('id',$id);
        $this->assign("userlist",$userlist);
        return $this->fetch("common/user_list");
    }
    
    /**
     * 会员详情
     * @return mixed|string
     */
    public function userdetails(){
        $uid = input('id',"",'intval');
        $user = db('user')->find($uid);
        $info = db('user')->where("gx={$uid}")->select();
        empty($info)?$user['type']=0:$user['type']=1;
        $this->assign('user',$user);
        return $this->fetch("common/user_details");
    }
    
    /**
     * 分享链接
     * @return mixed|string
     */
    public function shareurl(){
        $user = session("user");
        $op = input('op');
        if ($op == 'del'){
            $id = input('delid');
            $info = db("link") -> where("id={$id}") -> delete();
            if ($info){
                $this->success("操作成功！");
            }else {
                $this->error("操作失败！");
            }
        }
        $linkList = db("link") -> where("uid={$user['id']}") -> select();
        if ($linkList){
            foreach ($linkList as $k => $v){
                $linkList[$k]['link'] = url('Link/askLink', '',  true,  true).'?p='.$v['p'];
            }
        }
        $this->assign("linkList",$linkList);
        return $this->fetch("common/shareurl");
    }
    
    /**
     * 团队提现
     * @return mixed|string
     */
    public function teamtx(){
        return $this->fetch("common/teamtx");
    }
    
    /**
     * 团队统计
     * @return mixed|string
     */
    public function teamcount(){
        $userMod = db('user');
        $user = session('user');
        $user['soncount'] = $userMod->where("gx={$user['id']}")->count();
        if ($user['soncount'] > 0){
            $return = $this->GetSonInfo($user['id']);
            $user['online'] = $return['online'];
            $user['sonsum'] = $return['sum'];
            $user['sonmoneysum'] = $return['money'];
        }
        $this->assign("user",$user);
        return $this->fetch("common/teamcount");
    }
    
    /**
     * 获取当前用户下级信息
     * @param unknown $id
     * @return boolean|number[]|unknown[]
     */
    public function GetSonInfo($id){
        $userMod = db('user');
        $userSons = $userMod -> field('id,money,on_line') -> where("gx={$id}") -> select();
        if (empty($userSons)) return false;
        foreach ($userSons as $k => $v){
            $this->UserCount ++;
            $this->UserMoneySum += $v['money'];
            if ($v['on_line'] && time()-$v['on_line'] < 600) $this->UserOnLineSum ++;
            $son = $userMod -> field('id,money,on_line') -> where("gx={$v['id']}") -> select();
            if ($son){
                foreach ($son as $item){
                    $this->UserCount ++;
                    $this->UserMoneySum += $item['money'];
                    if ($v['on_line'] && time() < $v['on_line']) $this->UserOnLineSum ++;
                    $this->GetSonInfo($item['id']);
                }
            }
        }
        $return = array("sum"=>$this->UserCount,"money"=>$this->UserMoneySum,"online"=>$this->UserOnLineSum);
        return $return;
    }
}

